package com.neu.controller;

import com.neu.constant.ResultCodeEnum;
import com.neu.pojo.Cooperator;
import com.neu.service.CooperatorService;
import com.neu.service.ProvinceService;
import com.neu.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping(value = "/province", produces = {"application/json;charset=UTF-8"})
public class ProvinceController {

    @Resource
    private ProvinceService provinceService;

    @Resource
    private CooperatorService cooperatorService;

    @GetMapping("/dashboard")
    public Object dashboard() {
        log.info("查询首页信息...");
        return provinceService.dashboard();
    }

    @GetMapping("/info")
    public Object read(@RequestParam("id") int id, @RequestParam("name") String name){
        log.info("查询省份信息:id:{},name:{}",id,name);
        if (id <= 0 || id > 34) {
            return Result.error(ResultCodeEnum.PARAM_CHECK_ERROR);
        }
        return provinceService.queryProvince(id,name);
    }

    @GetMapping("/cooperator/list")
    public Object listCooperator(@RequestParam("provinceId") int provinceId){
        log.info("查询合作单位:{}",provinceId);
        if (provinceId <= 0 || provinceId > 34) {
            return Result.error(ResultCodeEnum.PARAM_CHECK_ERROR);
        }
        return cooperatorService.queryCooperatorList(provinceId);
    }

    @PostMapping("/cooperator/i")
    public Object insertCooperator(@RequestBody Cooperator cooperator){
        log.info("新增合作单位：{}",cooperator);
        return cooperatorService.addCooperator(cooperator);
    }

    @PostMapping("/cooperator/w")
    public Object updateCooperator(@RequestBody Cooperator cooperator){
        log.info("修改合作单位：{}",cooperator);
        return cooperatorService.updateCooperator(cooperator);
    }

    @PostMapping("/cooperator/d")
    public Object deleteCooperator(@RequestBody Cooperator cooperator){
        log.info("删除合作单位：{}",cooperator);
        return cooperatorService.deleteCooperator(cooperator);
    }

    @PostMapping("/cooperator/bi")
    public Object batchInsertCooperator(@RequestParam("file") MultipartFile file,
                                   @RequestParam("provinceId") int provinceId) throws IOException {
        log.info("导入合作单位:{}",provinceId);
        if (provinceId <= 0 || provinceId > 34) {
            return Result.error(ResultCodeEnum.PARAM_CHECK_ERROR);
        }
        return cooperatorService.batchInsertCooperator(file,provinceId);
    }



}
